MODULE boots
  USE GLOBVAR
  USE RANDOM
  USE STATISTICS
  USE MATRIX
  IMPLICIT NONE
  
CONTAINS
  
  SUBROUTINE boot(momentboot)
    IMPLICIT NONE
    REAL(8), INTENT(INOUT):: momentboot(:,:) !weight(:,:) !initial guess
    REAL(8) :: a(N_ind,n_testl),momb(n_boot,n_mom),momb2(n_boot,n_mom2),wei(n_mom2,n_mom2)
    REAL(8) :: inter(n_ind,n_levell,20,3),interl(n_ind,n_levell,3)
    REAL(8),ALLOCATABLE:: AVSE(:),cor(:,:)
    INTEGER :: dul(n_ind,n_levell),du(n_levell),ind(N_ind,n_levell),mmm,vindex(n_vnum+50) 
    INTEGER :: i,j,k,t,m,n,l,indexl(n_testl,2), artl(n_ind,11,20),mm(n_ind),ii,jj,kk 
    
    !!!!! start to calculate data moments!!!!!
    IF (myid==0) THEN
    !!! language tasks!!!!
   DO ii=1,n_boot
        jj=ii
        CALL SET_SEED(1438914432,234621+jj,1377777,123727)
     
                j=0
                k=0
                t=0
              DO i=1,N_vnum+50
                       vindex(i)=INT(SAMPLE_UNIFORM(1.0D0, DBLE(N_vnum+1)))
                       DO l=1,n_ind
                           IF (vid(l)==vindex(i)) t=l
                       END DO
                       j=k+vsum(t)
                       DO kk=k+1,j
                         IF (kk<=n_ind) mm(kk)=id_min(t)+j-kk
                          If(kk>n_ind) GO TO 36
                       END DO
                      k=j
             END DO
        !write(*,*) 
        !stop
36     DO i=1,N_ind

           mmboot(ii,i)=mm(i)
       END DO
       
    indexl=0
    DO i=1,N_ind
      DO j=1,N_testl
         IF (l_test(mm(i),j)>-90) THEN
             indexl(j,1)=indexl(j,1)+1
             IF (l_test(mm(i),j)>0.5) indexl(j,2)=indexl(j,2)+1
         END IF
         
      END DO
    END DO
    
    DO i=1,N_testl
        IF (indexl(i,1)>0.5) THEN
            momb(ii,i)=DBLE(indexl(i,2))/DBLE(indexl(i,1)) ! PASSING RATE FOR EACH LANGUAGE TASK
        ELSE 
            momb(ii,i)=-99
        END IF
    END DO
    
    !!!! generate artl variables !!!!
    artl=-1
    DO i=1,N_ind
        DO j=1,N_testl
            IF (l_test(mm(i),j)>-90) THEN
                k=levell(j)
                m=repl(mm(i),j)
                artl(i,k,m)=l_test(mm(i),j)
                inter(i,k,m,1:3)=x_testl(mm(i),j,1:3)
            END IF
        END DO
    END DO
    
    !!!! passing rate at each level!!!!

    DO j=N_testl+1,N_testl+N_levell
       m=0
       n=0 
       DO i=1,N_ind
           DO k=1,20
               IF (artl(i,j-N_testl,k)>-0.5) THEN
                   m=m+1
                   IF (artl(i,j-N_testl,k)==1) n=n+1
               END IF
           END DO 
       END DO
       IF (m>0) THEN
           momb(ii,j)=DBLE(n)/DBLE(m)
       ELSE
           momb(ii,j)=-99
       END IF
       
    END DO
    
    !!!! correlation across levels !!!!
    
    DO j=2,N_levell-1
      DO t=1,5
        DO k=1,5
          m=0
          n=0 
          DO i=1,N_ind 
             IF (artl(i,j,t)==1 .and. artl(i,j+1,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artl(i,j,t)==1 .and. artl(i,j+1,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,N_testl+N_levell+(j-2)*25+(t-1)*5+k)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,N_testl+N_levell+(j-2)*25+(t-1)*5+k)=-99
          END IF
        END DO
      END DO
    END DO
    
    DO j=2,N_levell
        l=0
      DO t=1,4
        DO k=t+1,5
          m=0
          n=0
          l=l+1
          DO i=1,N_ind 
             IF (artl(i,j,t)==1 .and. artl(i,j,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artl(i,j,t)==1 .and. artl(i,j,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,25*(N_levell-2)+N_testl+N_levell+(j-2)*10+l)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,25*(N_levell-2)+N_testl+N_levell+(j-2)*10+l)=-99
          END IF
        END DO
      END DO
    END DO
    
    DO j=2,N_levell-2
      DO t=1,5
        DO k=1,5
          m=0
          n=0 
          DO i=1,N_ind 
             IF (artl(i,j,t)==1 .and. artl(i,j+2,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artl(i,j,t)==1 .and. artl(i,j+2,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,25*(N_levell-2)+N_testl+N_levell+(N_levell-1)*10+(j-2)*25+(t-1)*5+k)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,25*(N_levell-2)+N_testl+N_levell+(N_levell-1)*10+(j-2)*25+(t-1)*5+k)=-99
          END IF
        END DO
      END DO
    END DO
    
    indexl=0
    DO i=1,N_ind
      DO j=1,N_testl
          a(i,j)=firstl(mm(i),2)-monthl(j)-DBLE(timel(j))/4.0d0
         IF (l_test(mm(i),j)>-90 .and. a(i,j)>-1.25d0) THEN
             indexl(j,1)=indexl(j,1)+1
             IF (l_test(mm(i),j)>0.5) indexl(j,2)=indexl(j,2)+1
         END IF
         
      END DO
    END DO
    
    DO i=1,N_testl
        IF (indexl(i,1)>0.5) THEN
            momb(ii,25*(2*N_levell-5)+N_testl+N_levell+(N_levell-1)*10+i)=DBLE(indexl(i,2))/DBLE(indexl(i,1)) ! PASSING RATE FOR EACH LANGUAGE TASK
        ELSE 
            momb(ii,25*(2*N_levell-5)+N_testl+N_levell+(N_levell-1)*10+i)=-99
        END IF
    END DO
    
    indexl=0
    DO i=1,N_ind
      DO j=1,N_testl
          a(i,j)=DBLE(firstl(mm(i),2)-monthl(j)-DBLE(timel(j))/4.0d0)
         IF (l_test(mm(i),j)>-90 .and. a(i,j)<-1.25d0) THEN
             indexl(j,1)=indexl(j,1)+1
             IF (l_test(mm(i),j)>0.5) indexl(j,2)=indexl(j,2)+1
         END IF
         
      END DO
    END DO


    
    DO i=1,N_testl
        IF (indexl(i,1)>0.5) THEN
            momb(ii,25*(2*N_levell-5)+2*N_testl+N_levell+(N_levell-1)*10+i)=DBLE(indexl(i,2))/DBLE(indexl(i,1)) ! PASSING RATE FOR EACH LANGUAGE TASK
        ELSE 
            momb(ii,25*(2*N_levell-5)+2*N_testl+N_levell+(N_levell-1)*10+i)=-99
        END IF
    END DO
    
    l=0
    DO j=2,N_levell
      DO t=1,5
          m=0
          n=0
          l=l+1
          DO i=1,N_ind 
             IF (artl(i,j,t)>-0.5) THEN   
                 m=m+1
                 IF (artl(i,j,t)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,25*(N_levell-2)+3*N_testl+N_levell+(N_levell-1)*10+25*(N_levell-3)+l)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,25*(N_levell-2)+3*N_testl+N_levell+(N_levell-1)*10+25*(N_levell-3)+l)=-99
          END IF
      END DO
    END DO

    mmm=25*(N_levell-2)+3*N_testl+N_levell+(N_levell-1)*10+25*(N_levell-3)+5*(N_levell-1)
    
    
    DUL=-99
    ind=-99
    DO i=1,N_ind
        DO k=2,N_levell
            DO j=1,20
                IF (artl(i,k,j)==1 .and. j==1) THEN
                    dul(i,k)=1
                    ind(i,k)=1
                ELSE IF (artl(i,k,j)==0 .and. j==1) THEN
                    dul(i,k)=j
                    ind(i,k)=0
                ELSE IF (j>1 ) THEN
                    IF (artl(i,k,j-1)==0 .and. artl(i,k,j)==0 .and. ind(i,k)==0) THEN
                       dul(i,k)=j
                       ind(i,k)=0
                       IF (j<20 .and. artl(i,k,j+1)==-1) THEN
                           dul(i,k)=j+1
                           ind(i,k)=1
                       END IF  
                    ELSE IF (artl(i,k,j-1)==0 .and. artl(i,k,j)==0 .and. ind(i,k)==1) THEN
                        dul(i,k)=dul(i,k)
                        ind(i,k)=1
                    ELSE IF (artl(i,k,j-1)==0 .and. artl(i,k,j)==1 .and. ind(i,k)==0) THEN
                        dul(i,k)=j
                        ind(i,k)=1
                    ELSE IF (artl(i,k,j-1)==0 .and. artl(i,k,j)==1 .and. ind(i,k)==1) THEN
                        dul(i,k)=dul(i,k)
                        ind(i,k)=1
                    END IF
                END IF
            END DO           
                
        END DO
    END DO
    
    DO k=1,n_levell
        m=0
        n=0
        DO i=1,n_ind
            IF (dul(i,k)>-90) THEN
               m=m+1
               n=n+dul(i,k)
            END IF
        END DO
        IF (m>0) THEN
          momb(ii,mmm+k)=DBLE(n)/DBLE(m)
        ELSE 
           momb(ii,mmm+k)=-99
        END IF
    END DO
    
    mmm=25*(N_levell-2)+3*N_testl+N_levell+(N_levell-1)*10+25*(N_levell-3)+5*(N_levell-1)+n_levell
    
    du=0
    DO k=1,n_levell
        DO i=1,N_ind
            IF (dul(i,k)>-90) THEN
                du(k)=du(k)+1
            END IF
        END DO
    END DO
    DO k=1,n_levell
        allocate(avse(du(k)))
        m=0
        DO i=1,n_ind
            IF (dul(i,k)>-90) THEN
                m=m+1
                avse(m)=dul(i,k)
            END IF
        END DO
        momb(ii,mmm+k)=STDEV_V(AVSE)
        DEALLOCATE(avse)
    END DO
    mmm=25*(N_levell-2)+3*N_testl+N_levell+(N_levell-1)*10+25*(N_levell-3)+5*(N_levell-1)+2*n_levell
    

    IND=0
    interl=0.0d0
    DO k=2,N_levell
        DO i=1,N_ind
          DO j=1,20
              IF (inter(i,k,j,1)>-90) THEN
                  ind(i,k)=ind(i,k)+1
                  interl(i,k,1)=interl(i,k,1)+inter(i,k,j,1)
                  interl(i,k,2)=interl(i,k,2)+inter(i,k,j,2)
                  interl(i,k,3)=interl(i,k,3)+inter(i,k,j,3)
              END IF
          END DO
          IF (ind(i,k)>0) THEN
               interl(i,k,1)=DBLE(interl(i,k,1))/DBLE(ind(i,k))
               interl(i,k,2)=DBLE(interl(i,k,2))/DBLE(ind(i,k))
               interl(i,k,3)=DBLE(interl(i,k,3))/DBLE(ind(i,k))
          END IF
        END DO
    END DO
    
    du=0
    DO k=1,n_levell
        DO i=1,N_ind
            IF (ind(i,k)>0 .AND. dul(i,k)>-90) THEN
            du(k)=du(k)+1          
            END IF
        END DO
    END DO
    
    DO k=1,n_levell
        allocate(cor(du(k),4))
        m=0
        DO i=1,n_ind
             IF (ind(i,k)>0 .AND. dul(i,k)>-90) THEN
                m=m+1
                cor(m,1)=interl(i,k,1)
                cor(m,2)=interl(i,k,2)
                cor(m,3)=interl(i,k,3)
                cor(m,4)=dul(i,k)
             END IF
        END DO
        IF (k>1) THEN
        momb(ii,mmm+3*(k-1)+1)=Correlation(cor(:,1),cor(:,4))
        momb(ii,mmm+3*(k-1)+2)=Correlation(cor(:,2),cor(:,4))
        momb(ii,mmm+3*(k-1)+3)=Correlation(cor(:,3),cor(:,4))
        ELSE 
            momb(ii,mmm+3*(k-1)+1)=-99
            momb(ii,mmm+3*(k-1)+2)=-99
            momb(ii,mmm+3*(k-1)+3)=-99
        END IF
        DEALLOCATE(cor)
    END DO
    mmm=25*(N_levell-2)+3*N_testl+N_levell+(N_levell-1)*10+25*(N_levell-3)+5*(N_levell-1)+5*n_levell
      
    
   END DO !ii
   
   momentboot=momb
   
       
           !
           !wei=Variance_Covariance(momb2)
           !!weight=Matrix_Inverse_symmetric(wei)
           !weight=Matrix_Inverse(wei)
           !OPEN(12,file="wei.out")
           !  DO j=1,N_mom2
           !   WRITE(12,'(2000F32.12)') (wei(j,i),i=1,n_mom2)
           !  END DO
           !CLOSE(12) 
           !OPEN(12,file="weight.out")
           !  DO j=1,N_mom2
           !   WRITE(12,'(2000F32.12)') (weight(j,i),i=1,n_mom2)
           !  END DO
           !CLOSE(12)  
           OPEN(12,file="momm.out")
             DO i=1,N_mom
              WRITE(12,'(2000F32.12)') (momb(j,i),j=1,n_boot)
             END DO
           CLOSE(12) 
              OPEN(12,file="mmboot.out")
             DO i=1,N_ind
              WRITE(12,'(2000I4)') (mmboot(j,i),j=1,n_boot)
             END DO
           CLOSE(12) 
      
    
    
    END IF  
        
    
    
    
    
  END SUBROUTINE boot
  
END MODULE boots